const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceList: [100, 200, 300, 500, 1000, 1500, 2000],
    state: 0,
    payable: 0,
    userInfo: [],
    recharge_account_balance: 0.00,
    otherMoney: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfo();
    app.showToast("充值可能会有延迟");
  },







  // 选择充值金额
  choosePrice: function (e) {
    const price = parseFloat(e.currentTarget.dataset.price);
    this.setData({
      otherMoney: "",
      state: e.currentTarget.dataset.in,
      payable: this.calculatePrice(price)
    })
  },

  // 其他金额
  otherPrice: function (e) {
    this.setData({
      state: "",
    })
    if (e.detail.value == "") {
      this.setData({
        payable: 0,
        otherMoney: "",
      })
    } else {
      this.setData({
        payable: this.calculatePrice(e.detail.value),
        otherMoney: e.detail.value
      })
    }
  },

  price: function (e) {
    let price = e.detail.value;
    this.setData({
      state: "",
      otherMoney: price,
      payable: this.calculatePrice(price)
    })
  },

  //计算金额
  calculatePrice(price) {
    let ratio = app.sub(1, this.ratio);
    return app.mul(price, ratio);
  },

  accountRecharge() {
    var that = this;
    let money = that.data.state !== "" ? that.data.priceList[that.data.state] : that.data.otherMoney;
    if (money <= 0) {
      app.showToast("请选择充值金额");
      return;
    }
    app.ajax({
      url: 'Salesman/Account/accountRecharge',
      data: {
        money
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (that.data.payable == 0) {
            app.showToast("支付成功", "success");
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          } else {
            var arritem = res.data.data;
            wx.requestPayment({
              'appId': arritem.appId,
              'timeStamp': arritem.timeStamp,
              'nonceStr': arritem.nonceStr,
              'package': arritem.package,
              'signType': arritem.signType,
              'paySign': arritem.paySign,
              'success': function (data) {
                app.showToast("支付成功!", "success", 2000, function () {});
                that.getInfo();
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000);
              },
              'fail': function (res) {
                that.setData({
                  on_off: true
                })
                app.showToast("取消支付", "none", 2000, function () {});
              }
            })
          }
        } else {
          app.showToast(res.data.message)
        }
      }
    });
  },

  pay() {
    var that = this;
    that.accountRecharge();
  },

  //获取账户余额
  getInfo() {
    var that = this;
    app.ajax({
      url: 'Salesman/Login/getInfo',
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.ratio = parseFloat(res.data.data.commission);
          that.setData({
            payable: that.data.state !== "" ? that.calculatePrice(that.data.priceList[that.data.state]) : "",
            recharge_account_balance: res.data.data.recharge_account_balance
          })
        }
      }
    });
  },
})